package com.rongke.web;

import com.rongke.commons.JsonResp;
import com.rongke.mapper.PropValueMapper;
import com.rongke.model.PropValue;
import com.rongke.service.PropValueService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * @PropValueController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/propValue")
@Transactional
@CrossOrigin(origins = "*",maxAge = 3600)
public class PropValueController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private PropValueService propValueService;

    /**
     * @添加
     * @param propValue
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addPropValue(@RequestBody PropValue propValue){
        log.debug("添加");
        propValue.setGmtDatetime(new Date());
        propValueService.insert(propValue);
        return JsonResp.ok(propValue);
    }

    /**
     * @param propValueList
     * @return 返回值JsonResp
     * @propValueList
     */
    @RequestMapping(value = "/addPropValueBatch", method = RequestMethod.POST)
    public JsonResp addPropValueBatch(@RequestBody List<PropValue> propValueList) {
        log.debug("批量添加规格值");
        if(propValueList.size()==0)
            return JsonResp.toFail("server:添加的规格值为空！");
        for(int i=0;i<propValueList.size();i++){
            propValueList.get(i).setGmtDatetime(new Date());
        }
        propValueService.insertBatch(propValueList);
        return JsonResp.ok(propValueList);
    }

    /**
     * @修改
     * @param propValue
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updatePropValue(@RequestBody PropValue propValue){
        log.debug("修改");
        propValue.setUptDatetime(new Date());
        propValueService.updateById(propValue);
        return JsonResp.ok(propValue);
    }
    /**
     * @根据id查找
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectPropValue(Long id){
        log.debug("查找");
        PropValue propValue = propValueService.selectById(id);
        return JsonResp.ok(propValue);
    }

    /**
     * @根据条件查找
     * @param propValue
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectPropValueByCondition(@RequestBody PropValue propValue){
        log.debug("根据条件查找");
        if(propValue==null)
            return JsonResp.toFail("参数不能为空");
        List<PropValue> propValueList=propValueService.selectByCondition(propValue);
        return JsonResp.ok(propValueList);
    }

}
